import { _decorator, Button, Component, Node, NodeEventType, Sprite, SpriteFrame } from 'cc';
import Core from 'db://app/Core';
import LoaderManager from 'db://app/manager/loader/LoaderManager';
import { app } from 'db://assets/app/app';
const { ccclass, property } = _decorator;

/**
 * 身体部位 
 */
export enum BodyPartType {
  /**
   * 身体
   */
  body=0,
  /**
   * 衣服
   */
  clothes,
  /**
   * 头发
   */
  hair,
  /**
   * 眼睛
   */
  eyes,
  /**
   * 丝袜
   */
  stockings,
  /**
   * 袜子
   */
  shoes,
}

/**
 * Tab 按钮数据
 */
export interface MainPageTabBtnData {
  /**
   * 部位
   */
  part: BodyPartType;

  /**
   * 是否是新的
   */
  isNew: boolean;

  /**
   * 是否选中
   */
  isSelect: boolean;
}

@ccclass('MainPageTabBtn')
export class MainPageTabBtn extends Component {
  //#region 节点
  /**
   * 按钮组件
   */
  @property(Button)
  private btn_comp: Button;

  @property(Node)
  private go_select: Node

  @property(Sprite)
  private img_icon: Sprite;

  @property(Node)
  private go_new: Node;

  //#endregion

  /**
   * 配置数据
   */
  public data: MainPageTabBtnData;

  /**
 * 点击回调
 */
  private call_back: (part_type: BodyPartType) => void;

  start() {
    this.setIcon(1);
    this.setSelect(false)
    this.setNew(false)

    this.btn_comp.node.off('click');
    this.btn_comp.node.on('click', this.onClickFunc, this);
  }

  private onClickFunc():void{
    this.call_back?.(this.data.part)
}


  update(deltaTime: number) {

  }

  /**
   * 设置数据
   * @param data tab 按钮数据
   */
  public setData(data: MainPageTabBtnData): void {
    this.data = data

    this._setData()
  }

  /**
   * 设置数据
   */
  private _setData(): void {
    this.setIcon(this.data.part);
    this.setSelect(this.data.isSelect)
    this.setNew(this.data.isNew)
  }

  /**
   * 设置图标
   * @param part 
   */
  private setIcon(part: number): void {
    let res_path = 'ui/hub/type/b_01/spriteFrame'

    switch (this.data.part) {
      case BodyPartType.body:
        res_path = 'ui/hub/type/b_01/spriteFrame'
        break;
      case BodyPartType.clothes:
        res_path = 'ui/hub/type/b_01/spriteFrame'
        break;
      case BodyPartType.hair:
        res_path = 'ui/hub/type/b_02/spriteFrame'
        break;
      case BodyPartType.eyes:
        res_path = 'ui/hub/type/b_06/spriteFrame'
        break;
      case BodyPartType.stockings:
        res_path = 'ui/hub/type/b_12/spriteFrame'
        break;
      case BodyPartType.shoes:
        res_path = 'ui/hub/type/b_10/spriteFrame'
        break;
    }

    app.manager.loader.load({
      path: res_path, bundle: 'res-native', type: SpriteFrame, onComplete: (spriteFrame) => {
        this.img_icon.spriteFrame = spriteFrame
      }
    })
  }

  /**
   * 设置选择状态
   * @param state 
   */
  public setSelect(state: boolean): void {
    this.go_select.active = state
  }


  /**
 * 添加函数
 * @param callfunc 
 */
  public addCallBack(callfunc: (part_type: BodyPartType) => void): void {
    this.call_back = callfunc;
  }

  /**
  * 设置选择状态
  * @param state 
  */
  public setNew(state: boolean): void {
    this.go_new.active = state
  }
}


